package com.dullfox.sbm.controller.sys;

import com.dullfox.sbm.common.auth.AuthIgnore;
import com.dullfox.sbm.common.bean.PageResult;
import com.dullfox.sbm.common.bean.Result;
import com.dullfox.sbm.common.global.RunException;
import com.dullfox.sbm.common.util.ConstUtil;
import com.dullfox.sbm.model.SysUser;
import com.dullfox.sbm.model.vo.SysUserVo;
import com.dullfox.sbm.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author: zengdy
 * Date: 2018/10/25
 */
@Controller
@RequestMapping(value = "/userController")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 保存
     */
    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestBody SysUser sysUser) {
        if (StringUtils.isBlank(sysUser.getName())) {
            throw new RunException(ConstUtil.ERROR_CODE, "name为空");
        }
        if (StringUtils.isBlank(sysUser.getPwd())) {
            throw new RunException(ConstUtil.ERROR_CODE, "pwd为空");
        }
        Result ret = new Result();
        SysUserVo vo = new SysUserVo();
        vo.setName(sysUser.getName());
        vo.setPwd(sysUser.getPwd());
        sysUserService.save(sysUser);
        return ret;
    }

    /**
     * 多条件分页查询
     */
    @PostMapping("/listData")
    @ResponseBody
    public PageResult<SysUser> listData(SysUserVo vo) {
        String status = vo.getStatus();
        if (StringUtils.isBlank(status)) {
            vo.setStatus(ConstUtil.STATUS.NORMAL.getStatus());
        }
        PageResult<SysUser> pr = sysUserService.listPage(vo);
        return pr;
    }

}
